﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.ModFlow.Domain.DomainLayer;
using HA.Data;
namespace HA.ModFlow.Controls.Windows
{
    /// <summary>
    /// winGlobalOption.xaml 的交互逻辑
    /// </summary>
    public partial class winGlobalOption : Window
    {
        public winGlobalOption(Project proj)
        {
            InitializeComponent();
            project = proj;
            ModFlowData mFData = proj.MfData;
            this._mfData = mFData;
            this._mdOption = mFData.ModelOption;
            //控件绑定
            Binding binding=new Binding();
            binding.Source = _mfData.ModelOption;
            binding.Path = new PropertyPath("RunType");
            binding.Converter = new ModFlowRunTypeToBoolConverter();
            binding.Mode = BindingMode.OneWay;
            this.rbTransient.SetBinding(RadioButton.IsCheckedProperty,binding);

            if (this.rbTransient.IsChecked == false)
            {
                this.rbSteady.IsChecked = true;
            }
            this._stressPeriods = new List<StressPeriod>();
            foreach (StressPeriod item in _mfData.StressPeriods.InnerList)
            {               
                this._stressPeriods.Add((StressPeriod)item.Clone());
            }
            this._HNOFLO = this._mfData.HNOFLO;

            this.txtTitle1.Text = this._mfData.Title1;
            this.txtTitle2.Text = this._mfData.Title2;

            Binding bin = new Binding();
            bin.Source = this;
            bin.Path = new PropertyPath("HNOFLO");      
            this.txtHNOFLO.SetBinding(TextBox.TextProperty, bin);
        }
        Project project;

        private double _HNOFLO;

        public double HNOFLO
        {
            get { return _HNOFLO; }
            set { _HNOFLO = value; }
        }

     
        ModFlowData _mfData;

        //供本窗体确定时再更改应力期设置为子窗体的设置
        List<StressPeriod> _stressPeriods;
      

        //供保存子窗体Unit的设置
        ModelOption _mdOption;

        private void btnStrPeriods_Click(object sender, RoutedEventArgs e)
        {
            if (this._stressPeriods == null)
            {
                winStressPeriod winSPeriond = new winStressPeriod(this._mfData.StressPeriods.InnerList, _mdOption==null? _mfData.ModelOption.Units:_mdOption.Units);
                winSPeriond.Owner = this;
                winSPeriond.ResizeMode =ResizeMode.NoResize;
                winSPeriond.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                winSPeriond.ShowDialog();
                if (winSPeriond.DialogResult == true)
                {
                    this._stressPeriods = winSPeriond.StressPeriod;
                
                }
            }

            else
            {
                winStressPeriod winSPeriond = new winStressPeriod(this._stressPeriods, _mdOption == null ? _mfData.ModelOption.Units : _mdOption.Units);
                winSPeriond.Owner = this;
                winSPeriond.ResizeMode = ResizeMode.NoResize;
                winSPeriond.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                winSPeriond.ShowDialog();
                if (winSPeriond.DialogResult == true)
                {
                    this._stressPeriods = winSPeriond.StressPeriod;
                 
                }
            }
            
        }

        private void btnPackages_Click(object sender, RoutedEventArgs e)
        {
            winPackages winPackages = new winPackages(this._mfData.ModelOption,this._mfData);
            winPackages.Owner = this;
            winPackages.ResizeMode = ResizeMode.NoResize;
            winPackages.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            winPackages.ShowDialog();
        }

        private void btnUnits_Click(object sender, RoutedEventArgs e)
        {
            if (this._mdOption == null)
            {
                winUnits winUnits = new winUnits(this._mfData.ModelOption);
                winUnits.Owner = this;
                winUnits.ResizeMode = ResizeMode.NoResize;
                winUnits.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                winUnits.ShowDialog();
                if (winUnits.DialogResult == true)
                {
                    this._mdOption = winUnits.MdOption;
                }
            }
            else
            {
                winUnits winUnits = new winUnits(this._mdOption);
                winUnits.Owner = this;
                winUnits.ResizeMode = ResizeMode.NoResize;
                winUnits.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                winUnits.ShowDialog();
                if (winUnits.DialogResult == true)
                {
                    this._mdOption = winUnits.MdOption;
                }
            }
        }

        private void btnIbound_Click(object sender, RoutedEventArgs e)
        {
            //winGridIBound winIbound = new winGridIBound(this._mfData.GridData);
            //winIbound.ShowDialog();
            
            winCellCenterDSEdit win = new winCellCenterDSEdit(this._mfData.GridData.GetIBoundDs(),project);
            win.Owner = this;
            win.ResizeMode = ResizeMode.NoResize;
            win.Unit += "无量纲";
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            if (win.ShowDialog() == true)
            {

                this._mfData.GridData.SetIBoundDs(win.PcDs.Dataset);
                if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                    project.CurrentDSData = this._mfData.GridData.GetIBoundDs();
            }



        }

        private void btnHeads_Click(object sender, RoutedEventArgs e)
        {
            //winInitHead winInitHead = new winInitHead(this._mfData.GridData);
            //winInitHead.ShowDialog();

            winCellCenterDSEdit win = new winCellCenterDSEdit(this._mfData.GridData.GetIHeadDs(), project);
            win.Owner = this;
            win.ResizeMode = ResizeMode.NoResize;
            win.Unit += "(" + Domain.DomainLayer.UnitConverter.GetLength(_mdOption.Units.Length) + ")";
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            if (win.ShowDialog() == true)
            {

                this._mfData.GridData.SetIHeadDs(win.PcDs.Dataset);
                if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                    project.CurrentDSData = this._mfData.GridData.GetIHeadDs();
            }

        }

        private void btnTopElevation_Click(object sender, RoutedEventArgs e)
        {
            //winGridElevation winElevation = new winGridElevation(this._mfData.GridData);
            //winElevation.ShowDialog();
            winCellCenterDSEdit win = new winCellCenterDSEdit(this._mfData.GridData.GetTopElevationDs(), project);
            win.Owner = this;
            win.ResizeMode = ResizeMode.NoResize;
            win.Unit += "(" + Domain.DomainLayer.UnitConverter.GetLength(_mdOption.Units.Length) + ")";
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            if (win.ShowDialog()==true)
            {

                this._mfData.GridData.SetTopElevationDS(win.PcDs.Dataset);
                if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                    project.CurrentDSData = this._mfData.GridData.GetTopElevationDs();
            }



        }


        private void btnBotElevation_Click(object sender, RoutedEventArgs e)
        {
            //winGridElevation winElevation = new winGridElevation(this._mfData.GridData);
            //winElevation.ShowDialog();

            winCellCenterDSEdit win = new winCellCenterDSEdit(this._mfData.GridData.GetBotElevationDs(), project);
            win.Owner = this;
            win.ResizeMode = ResizeMode.NoResize;
            win.Unit += "(" + Domain.DomainLayer.UnitConverter.GetLength(_mdOption.Units.Length) + ")";
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            if (win.ShowDialog() == true)
            {

                this._mfData.GridData.SetBotElevationDS(win.PcDs.Dataset);
                if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                    project.CurrentDSData = this._mfData.GridData.GetBotElevationDs();
            }



        }




        private void btnConBeds_Click(object sender, RoutedEventArgs e)
        {
            winConfiningBed winConfiningBed = new winConfiningBed(this .project);
            winConfiningBed.Owner = this;
            winConfiningBed.ResizeMode = ResizeMode.NoResize;
            winConfiningBed.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            winConfiningBed.ShowInTaskbar = false;
            winConfiningBed.ShowDialog();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this._mfData.Title1 = this.txtTitle1.Text;
            this._mfData.Title2 = this.txtTitle2.Text;
            this._mfData.HNOFLO = this._HNOFLO;
            //存储ModelType
            if (this.rbSteady.IsChecked == true)
            {
                this._mfData.ModelOption.RunType = MODFLOWRUNTYPE.STEADYSTATE;
            }
            else
            {
                this._mfData.ModelOption.RunType = MODFLOWRUNTYPE.TRANSIENT;
            }

            //存储Unit设置,为空则保持原设置，不为空即使未更改也重新复制一次无妨
            if (this._mdOption != null)
            {
                this._mfData.ModelOption.Units.Length = this._mdOption.Units.Length;
                this._mfData.ModelOption.Units.Time = this._mdOption.Units.Time;
            }

            //存储应力期设置

                this._mfData.StressPeriods.ResetStressPeriods(this._stressPeriods);
          
            this.DialogResult = true;
            this.Close();
        }

        private void rbTransient_Checked(object sender, RoutedEventArgs e)
        {
            //this.btnStrPeriods.IsEnabled = true;
        }

        private void rbTransient_Unchecked(object sender, RoutedEventArgs e)
        {
            //this.btnStrPeriods.IsEnabled = false;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }


    }

    public class ModFlowRunTypeToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((MODFLOWRUNTYPE)value == MODFLOWRUNTYPE.TRANSIENT)
            {
                return true;
            }
            else 
                return false;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((bool)value == true)
            {
                MODFLOWRUNTYPE et = MODFLOWRUNTYPE.TRANSIENT;
                return et;
            }
            else
            {
                MODFLOWRUNTYPE et = MODFLOWRUNTYPE.STEADYSTATE;
                return et;
            }
            
        }
    }
}
